from keras.models import load_model
from keras_preprocessing.sequence import pad_sequences
import numpy as np

# 加载模型
model = load_model('emotion_classification_model.h5')

# 定义词汇表大小、输入序列长度和嵌入维度，需要与训练模型时的设置保持一致
max_features = 5000
max_len = 100
embedding_dim = 50

# 定义情绪类别标签
emotion_labels = ['害怕', '惊讶', '伤心', '厌恶', '生气', '高兴']

# 输入文本
input_text = "这是一个令人惊喜的好消息"

# 预处理输入文本
input_sequence = imdb.get_word_index()
input_sequence = [input_sequence[word.lower()] if word.lower() in input_sequence else 0 for word in input_text.split()]
input_sequence = pad_sequences([input_sequence], maxlen=max_len)

# 进行情绪分类预测
prediction = model.predict(input_sequence)[0]
emotion_index = np.argmax(prediction)
emotion_label = emotion_labels[emotion_index]

# 打印预测结果
print(f"输入文本: {input_text}")
print(f"情绪类别: {emotion_label}")
print(f"情绪概率分布: {prediction}")
